package com.colin.aiyi.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.colin.aiyi.ui.IViewAc;
import com.colin.aiyi.eventbus.EventFloatButton;
import com.colin.aiyi.fragment.IFragmentView.IFragmentView;
import com.colin.aiyi.presenter.AbsP;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public abstract class BaseFragment extends Fragment implements TabLayout.OnTabSelectedListener,IFragmentView{
    public Context context;
    public View view;
    public IViewAc iMainView;
    public AbsP iPresenter;
    public Gson gson=new Gson();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            this.context=getContext();
        } catch (Exception e) {
            e.printStackTrace();
            this.context=getActivity();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }
    /*个人建议在onPause注册EventBus(将当前Activity注册为事件订阅者)
     *不影响功能的情况下提早解除注册，尽可能少的占用内存
     */
    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    public  <E > void sendMsgToView(E e){
        EventBus.getDefault().post(e);
    }
    public  <E > void sendMsgToView(Class<E> e,String json){
        E baseResponse=gson.fromJson(json, e);
        if (baseResponse!=null)
        {
            EventBus.getDefault().post(baseResponse);
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {


    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }




    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventListner(EventFloatButton eventFloatButton){
    }



    @Override
    public void goToTop(boolean b) {

    }

    public void showMsg(String message) {
        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
    }

    /** Fragment当前状态是否可见 */
    protected boolean isVisible;


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }


    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }


    /**
     * 不可见
     */
    protected void onInvisible() {


    }
    public void hideSoftInputFromWindow(Context context,View view){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }
    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();
}
